<?php
namespace app\shawting\controller;

use think\Validate;

class Notice extends Base
{
    public function index()
    {
        $search = input('search')?input('search'):"";
        $where["title"] = ["like","%{$search}%"];
        $arr = db("city_notice")->where($where)->order("create_time DESC")->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $this->assign("arr",$arr);
        $this->assign("search",$search);
        return $this->fetch();
    }

    public function ajax_add()
    {
        $data = input();
        $datas = [
            "title" => $data['title'],
            "context" => $data['context'],
            "create_time" => date("Y-m-d H:i:s",time()),
        ];
        $validate = new Validate([
                "title" => "require",
                "context" => "require",
            ],
            [
                "title.require" => "公告标题不能为空",
                "context.require" => "公告内容不能为空",
            ]
        );
        if(!$validate->check($datas)){
            $this->error($validate->getError());
        }
        $re = db("city_notice")->insert($datas);
        if($re){
            $this->success("添加成功");
        }else{
            $this->error('添加失败');
        }
    }

    public function lookContent(){
        $id = input("id");
        if(!is_numeric($id)){
           return $this->error('查询数据格式不正确');
        }
        $result = db("city_notice")->where(["id"=>$id])->find();
        if(!$result){
            return $this->error("暂无数据");
        }
        $this->success(null,null,$result);
    }













}